﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameLibrary;
using AimGameEngine.EditorObject;

namespace AimGameEngine.NodeEditorObject
{
    public class NodeCamera : NodeBaseControl
    {
        public Vector2 Position;
        public Vector2 Scale;
        public float Angle;

        protected String NamePosition= "NULL";
        protected String NameScale = "NULL";
        protected String NameRotation = "NULL";

        public NodeCamera(SpriteBatch inputspritebatch, Camera inputcamera, NodeData inputData
            , List<IDraw> MyListDraw, List<IUpdateMainTime> MyListUpdateTime, Dictionary<String, NodeObject> MyListObject, Dictionary<String, NodeObject> MyListType)
            : base(inputspritebatch, inputcamera, inputData, MyListDraw,MyListUpdateTime, MyListObject, MyListType)
	    {
            Position = Vector2.Zero;
            Scale = Vector2.Zero;
            Angle = 0f;
            InputMode = NodeInputMode.InputMode_1;

            Connecter outputCamera = new Connecter(inputspritebatch, inputcamera, Syco.Output, NodeTypeValue.Camera, this);
            ListConnectpoint.Add(outputCamera);

            Connecter inputPosition = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Vector2, this);
            ListConnectpoint.Add(inputPosition);

            Connecter inputScale = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Vector2, this);
            ListConnectpoint.Add(inputScale);

            Connecter inputRotation = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Float, this);
            ListConnectpoint.Add(inputRotation);
        }

        public override void SetupInput(Connecter Thisconnect, Connecter connectOutput)
        {
            if (InputMode == NodeInputMode.InputMode_1)
            {
                switch (ListConnectpointInput.IndexOf(Thisconnect))
                {
                    case 0: NamePosition = connectOutput.MyNode.MyData.MyName; break;
                    case 1: NameScale = connectOutput.MyNode.MyData.MyName; break;
                    case 2: NameRotation = connectOutput.MyNode.MyData.MyName; break;
                }
                MyData.Data = NamePosition + "|" + NameScale + "|" + NameRotation;
            }
        }

        public override void ClearInput(Connecter ThisInputConnect, Connecter ThisConnectClear, string NameThisClear)
        {
            //base.ClearInput(ThisConnectClear, NameThisClear);
            MyData.Data = MyData.Data.Replace(NameThisClear, "NULL");
        }

        public override void ShowProprety(ArrayList Temp, CallBack input)
        {
            base.ShowProprety(Temp, input);
            PropretyCamera propretycamera = new PropretyCamera(this);
            //propretycamera.Deactivate += new EventHandler(input.Deactivate);
            propretycamera.okButton.Click += new EventHandler(input.Deactivate);
            propretycamera.ShowDialog();
            
        }

        public override void SetupProprety()
        {
            base.SetupProprety();

            if (InputMode == NodeInputMode.InputMode_1)
            {
                Connecter inputPosition = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Vector2, this);
                ListConnectpoint.Add(inputPosition);

                Connecter inputScale = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Vector2, this);
                ListConnectpoint.Add(inputScale);

                Connecter inputRotation = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Float, this);
                ListConnectpoint.Add(inputRotation);
            }
            else MyData.Data = "NULL";
            
            this.AfterSetupProprety();
        }

        protected override Dictionary<string, NodeObject> GetDictionaryNode(Dictionary<string, NodeObject> TempNode)
        {
            if (NamePosition != null)
                if (this.MyDictionaryObject.ContainsKey(NamePosition))
                    TempNode.Add(NamePosition, this.MyDictionaryObject[NamePosition]);

            if (NameScale != null)
                if (this.MyDictionaryObject.ContainsKey(NameScale))
                    TempNode.Add(NameScale, this.MyDictionaryObject[NameScale]);

            if (NameRotation != null)
                if (this.MyDictionaryObject.ContainsKey(NameRotation))
                    TempNode.Add(NameRotation, this.MyDictionaryObject[NameRotation]);

            return base.GetDictionaryNode(TempNode);
        }

        public override void SaveData(System.Collections.ArrayList Thislist, SaveMode inputsavemode)
        {
            base.SaveData(Thislist,inputsavemode);

            Thislist.Add(InputMode);
            Thislist.Add(Position);
            Thislist.Add(Scale);
            Thislist.Add(Angle);
        }

        public override void LoadData(Identification getdata, int IndexData)
        {
            base.LoadData(getdata, IndexData);

            if (getdata.Items.GetValue(IndexData + 1).GetType() == typeof(NodeInputMode))
                InputMode = (NodeInputMode)getdata.Items.GetValue(IndexData + 1);

            if (getdata.Items.GetValue(IndexData + 2).GetType() == typeof(Vector2))
                Position = (Vector2)getdata.Items.GetValue(IndexData + 2);

            if (getdata.Items.GetValue(IndexData + 3).GetType() == typeof(Vector2))
                Scale = (Vector2)getdata.Items.GetValue(IndexData + 3);

            if (getdata.Items.GetValue(IndexData + 4).GetType() == typeof(float))
                Angle = (float)getdata.Items.GetValue(IndexData + 4);
        }
    }
}
